/*
* @(#)StepInformationWindow.java
*
* Copyright (c) 2004 DCIVision Ltd
* All rights reserved.
*
* This software is the confidential and proprietary information of DCIVision
* Ltd ("Confidential Information").  You shall not disclose such Confidential
* Information and shall use it only in accordance with the terms of the license
* agreement you entered into with DCIVision Ltd.
*/

package com.dcivision.workflow.applet.editor;

/**
  WorkflowErrorConstant.java

  This class is to store all error code constants which used in systems.

    @author          Zealot
    @company         DCIVision Limited
    @creation date   1/13/2005
    @version         $Revision: 1.2 $
*/

import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.net.URLEncoder;
import java.util.Calendar;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import org.jgraph.graph.DefaultEdge;

import com.dcivision.workflow.applet.AppletResource;
import com.dcivision.workflow.applet.ListObject;
import com.dcivision.workflow.applet.WindowUtilities;
import com.dcivision.workflow.applet.net.HttpOperation;
import com.dcivision.workflow.applet.net.Utility;
import com.dcivision.workflow.bean.MtmWorkflowProgressPriorityRule;

public class PirorityRule extends JDialog {

  public static final String REVISION = "$Revision: 1.2 $";
  JPanel panel1 = new JPanel();
  JButton jButton1 = new JButton();
  DefaultEdge edge = null;
  String[] routeLable = null;
  JLabel jLabel2 = new JLabel();
  JLabel jLabel1 = new JLabel();
  JLabel jLabel3 = new JLabel();
  JLabel jLabel4 = new JLabel();
  JLabel jLabel5 = new JLabel();
  JComboBox jCBEscalateTrigger = new JComboBox();
  JTextField jTFEscalateAtAmt = new JTextField();
  JComboBox jCBEscalateAtField = new JComboBox();
  JComboBox jCBEscalateLevel = new JComboBox();
  JCheckBox jCBEmail = new JCheckBox();
  JCheckBox jCBSystem = new JCheckBox();
  JButton jButton2 = new JButton();
  JScrollPane jScrollPane1 = new JScrollPane();
  JLabel jLabel6 = new JLabel();
  JList jLRule = new JList();
  DefaultListModel listRuleModel = new DefaultListModel();
  JButton jButton3 = new JButton();
  HttpOperation httpOperation = null;
  String priorityRuleListValue = null;
  StepInformationWindow stepInformationWindow=null;
  public PirorityRule(HttpOperation httpOperation,String priorityRuleListValue,StepInformationWindow stepInformationWindow, String title, boolean modal) {
    super(stepInformationWindow, title, modal);
    try {
      this.stepInformationWindow = stepInformationWindow;
      this.httpOperation = httpOperation;
      this.priorityRuleListValue=priorityRuleListValue;
      jbInit();
      pack();
    }
    catch(Exception ex) {
      ex.printStackTrace();
    }
  }

  private void jbInit() throws Exception {
    panel1.setLayout(null);
    jButton1.setBounds(new Rectangle(224, 249, 84, 25));
    jButton1.setText(AppletResource.lableArr[24]);
    jButton1.addActionListener(new PirorityRule_jButton1_actionAdapter(this));

    jLabel2.setText(AppletResource.lableArr[164]);
    jLabel2.setBounds(new Rectangle(27, 54, 103, 24));
    jLabel1.setText(AppletResource.lableArr[163]);
    jLabel1.setBounds(new Rectangle(30, 22, 307, 19));
    jLabel3.setText(AppletResource.lableArr[165]);
    jLabel3.setBounds(new Rectangle(174, 54, 107, 22));
    jLabel4.setText(AppletResource.lableArr[166]);
    jLabel4.setBounds(new Rectangle(285, 54, 96, 20));
    jLabel5.setText(AppletResource.lableArr[167]);
    jLabel5.setBounds(new Rectangle(380, 52, 91, 24));
    jCBEscalateTrigger.setBounds(new Rectangle(26, 87, 137, 22));
    jTFEscalateAtAmt.setText("0");
    jTFEscalateAtAmt.setBounds(new Rectangle(176, 87, 32, 22));
    jCBEscalateAtField.setBounds(new Rectangle(210, 87, 74, 20));
    jCBEscalateLevel.setBounds(new Rectangle(288, 86, 58, 21));
    jCBEmail.setText(AppletResource.lableArr[173]);
    jCBEmail.setBounds(new Rectangle(380, 83, 69, 21));
    jCBSystem.setText(AppletResource.lableArr[174]);
    jCBSystem.setBounds(new Rectangle(456, 81, 78, 25));
    jButton2.setBounds(new Rectangle(536, 83, 55, 22));
    jButton2.setText(AppletResource.lableArr[5]);
    jButton2.addActionListener(new PirorityRule_jButton2_actionAdapter(this));
    jScrollPane1.setBounds(new Rectangle(29, 153, 491, 86));
    jLabel6.setText(AppletResource.lableArr[175]);
    jLabel6.setBounds(new Rectangle(29, 128, 332, 22));
    jButton3.setBounds(new Rectangle(521, 213, 84, 22));
    jButton3.setSelected(false);
    jButton3.setText(AppletResource.lableArr[6]);
    jButton3.addActionListener(new PirorityRule_jButton3_actionAdapter(this));
    getContentPane().add(panel1);
    panel1.add(jButton1, null);
    panel1.add(jLabel1, null);
    panel1.add(jLabel2, null);
    panel1.add(jLabel3, null);
    panel1.add(jCBEscalateTrigger, null);
    panel1.add(jLabel4, null);
    panel1.add(jTFEscalateAtAmt, null);
    panel1.add(jCBEscalateAtField, null);
    panel1.add(jCBEmail, null);
    panel1.add(jLabel5, null);
    panel1.add(jCBSystem, null);
    panel1.add(jButton2, null);
    panel1.add(jScrollPane1, null);
    jScrollPane1.getViewport().add(jLRule, null);
    panel1.add(jLabel6, null);
    panel1.add(jButton3, null);
    panel1.add(jCBEscalateLevel, null);
    initList();
  }

  public void initList(){
    String[][] escalateTriggerArr = {{MtmWorkflowProgressPriorityRule.AFTER_TASK_ARRIVED,AppletResource.lableArr[168]},
        {MtmWorkflowProgressPriorityRule.BEFORE_DUE_DATE,AppletResource.lableArr[169]}
    };
    for(int i=0;i<escalateTriggerArr.length;i++){
      ListObject listObject = new ListObject();
      listObject.setID(escalateTriggerArr[i][0]);
      listObject.setName(escalateTriggerArr[i][1]);
      jCBEscalateTrigger.addItem(listObject);
    }
    String[][] escalateAtFieldArr = {{Calendar.DATE+"",AppletResource.lableArr[170]},
        {Calendar.HOUR+"",AppletResource.lableArr[171]},
        {Calendar.MINUTE+"",AppletResource.lableArr[172]}
    };
    for(int i=0;i<escalateAtFieldArr.length;i++){
      ListObject listObject = new ListObject();
      listObject.setID(escalateAtFieldArr[i][0]);
      listObject.setName(escalateAtFieldArr[i][1]);
      jCBEscalateAtField.addItem(listObject);
    }
    String[][] escalateLevelArr = {{"1","+1"},
        {"2","+2"},
        {"3","+3"}
    };
    for(int i=0;i<escalateLevelArr.length;i++){
      ListObject listObject = new ListObject();
      listObject.setID(escalateLevelArr[i][0]);
      listObject.setName(escalateLevelArr[i][1]);
      jCBEscalateLevel.addItem(listObject);
    }
    jLRule.setModel(listRuleModel);
    addRuleByRuleItem(priorityRuleListValue);

  }

  public void addRuleByRuleItem(String priorityRuleListValue){
    try{
      if(priorityRuleListValue==null){
        return;
      }
      String[] ruleList = WindowUtilities.splitString(priorityRuleListValue,"|");
      for(int i=0;i<ruleList.length;i++){
        String[] ruleItem = WindowUtilities.splitString(ruleList[i],",");
        String priorityRule=ruleItem[0]+" ";
        if(Calendar.DATE == Integer.parseInt(ruleItem[1])) {
          priorityRule+=AppletResource.lableArr[170];
        }else if (Calendar.HOUR == Integer.parseInt(ruleItem[1])){
          priorityRule+=AppletResource.lableArr[171];
        }else{
          priorityRule+=AppletResource.lableArr[172];
        }
        priorityRule+=" ";
        if(MtmWorkflowProgressPriorityRule.AFTER_TASK_ARRIVED.equals(ruleItem[2])){
          priorityRule+=AppletResource.lableArr[168];
        }else{
          priorityRule+=AppletResource.lableArr[169];
        }
        priorityRule+=" ";
        String level;
        if(Integer.parseInt(ruleItem[3])>0 ){
          level="+"+ruleItem[3];
        }else{
          level=ruleItem[3];
        }
        String url = "/workflow/appletMiddleware?dataType=GET_RESOURCEBYCODE";
        url+="&CODE="+AppletResource.messageCodeArr[8]+"&ARG1="+URLEncoder.encode(level,"UTF-8");
        priorityRule+=WindowUtilities.getObjectFromServlet(httpOperation,url);
        if(!"null".equals(ruleItem[4]) || !"null".equals(ruleItem[5]) ){
          priorityRule+=", "+AppletResource.lableArr[167]+" ";
          if(!"null".equals(ruleItem[4])){
            priorityRule+=AppletResource.lableArr[173];
          }
          if(!"null".equals(ruleItem[5])){
            if(!"null".equals(ruleItem[4])){
              priorityRule+=" "+AppletResource.lableArr[176]+" ";
            }            
            priorityRule+=AppletResource.lableArr[174];
          }
        }
        ListObject listObject = new ListObject();
        listObject.setID(ruleList[i]);
        listObject.setName(priorityRule);
        listRuleModel.addElement(listObject);

      }
    }catch(Exception ex){
    }
  }

  public void initData(){
    for(int i=0;i<routeLable.length;i++){
      ListObject listObject = new ListObject();
      listObject.setID(i+"");
      listObject.setName(routeLable[i]);
      //jCBRoute.addItem(listObject);

    }
  }
  public DefaultEdge getEdge(){
    return this.edge;
  }

  public void setEdge(DefaultEdge edge){
    this.edge = edge;
  }

  public void setrouteLable(String[] routeLable){
    this.routeLable = routeLable;
  }

  void jButton1_actionPerformed(ActionEvent e) {
    priorityRuleListValue="";
    int length = listRuleModel.getSize();
    for(int i=0;i<length;i++){
      ListObject listObject=(ListObject)listRuleModel.getElementAt(i);
      if(i==0){
        priorityRuleListValue+=listObject.getID();
      } else {
        priorityRuleListValue+="|"+listObject.getID();
      }
    }
    stepInformationWindow.priorityRuleListValue=priorityRuleListValue;
    this.dispose();
  }

  void setHttpOperation(HttpOperation httpOperation){
    this.httpOperation = httpOperation;
  }

  void jButton2_actionPerformed(ActionEvent e) {
    try{
/*      String priorityRule="";
      priorityRule+=jTFEscalateAtAmt.getText()+" ";
      priorityRule+=((ListObject)jCBEscalateAtField.getSelectedItem()).getName()+" ";
      priorityRule+=((ListObject)jCBEscalateTrigger.getSelectedItem()).getName()+", ";
      //WindowUtilities.getObjectFromServlet();
      String url = "/workflow/appletMiddleware?dataType=GET_RESOURCEBYCODE";
      String level = ((ListObject)jCBEscalateLevel.getSelectedItem()).getName();
      url+="&CODE="+AppletResource.messageCodeArr[8]+"&ARG1="+URLEncoder.encode(level,"UTF-8");
      priorityRule+=WindowUtilities.getObjectFromServlet(httpOperation,url)+", ";
      if(jCBEmail.isSelected()||jCBSystem.isSelected()){
        priorityRule+=AppletResource.lableArr[167]+" ";
        if(jCBEmail.isSelected()){
          priorityRule+=AppletResource.lableArr[173];
        }
        if(jCBSystem.isSelected()){
          priorityRule+=" "+AppletResource.lableArr[176]+" ";
          priorityRule+=AppletResource.lableArr[174];
        }
      }*/
      String priorityRuleValue="";
      if(Utility.isEmpty(jTFEscalateAtAmt.getText())){
        stepInformationWindow.getEditor().outputMessage(AppletResource.lableArr[178],true);
        return;
      }
      priorityRuleValue+=jTFEscalateAtAmt.getText()+",";
      priorityRuleValue+=((ListObject)jCBEscalateAtField.getSelectedItem()).getID()+",";
      priorityRuleValue+=((ListObject)jCBEscalateTrigger.getSelectedItem()).getID()+",";
      priorityRuleValue+=((ListObject)jCBEscalateLevel.getSelectedItem()).getID()+",";
      if(jCBEmail.isSelected()){
        priorityRuleValue+=MtmWorkflowProgressPriorityRule.NOTIFY_BY_EMAIL+",";
      }else{
        priorityRuleValue+="null,";
      }
      if(jCBSystem.isSelected()){
        priorityRuleValue+=MtmWorkflowProgressPriorityRule.NOTIFY_BY_SYSTEM;
      }else{
        priorityRuleValue+="null";
      }
      addRuleByRuleItem(priorityRuleValue);
    }catch(Exception ex){
    }


  }

  void jButton3_actionPerformed(ActionEvent e) {
    int index = jLRule.getSelectedIndex();
    listRuleModel.remove(index);

  }
}

class PirorityRule_jButton2_actionAdapter implements java.awt.event.ActionListener {
  PirorityRule adaptee;

  PirorityRule_jButton2_actionAdapter(PirorityRule adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton2_actionPerformed(e);
  }
}

class PirorityRule_jButton3_actionAdapter implements java.awt.event.ActionListener {
  PirorityRule adaptee;

  PirorityRule_jButton3_actionAdapter(PirorityRule adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton3_actionPerformed(e);
  }
}

class PirorityRule_jButton1_actionAdapter implements java.awt.event.ActionListener {
  PirorityRule adaptee;

  PirorityRule_jButton1_actionAdapter(PirorityRule adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton1_actionPerformed(e);
  }
}
